#include "ringqueue_cp.hpp"
#include <ctime>
#include <unistd.h>
#include <stdlib.h>
#include <iostream>

void *Consumer(void *args)
{
    RingQueue<int> *rq = (RingQueue<int> *)args;
    while (true)
    {
        sleep(1);
        // 接受任务
        int data = 0;
        // 处理任务
        rq->Pop(&data);
        std::cout << "Consumer -> " << data << std::endl;
    }

    return nullptr;
}

void *Producer(void *args)
{
    RingQueue<int> *rq = (RingQueue<int> *)args;
    while (true)
    {
        // 产生任务
        //sleep(1);
        int data = rand() % 10 + 1;
        // 发送任务
        rq->Push(data);
        std::cout << "Producer -> " << data << std::endl;
    }

    return nullptr;
}

int main()
{
    srand(time(nullptr));
    pthread_t c, p;

    RingQueue<int> *rq = new RingQueue<int>(5);

    pthread_create(&c, nullptr, Consumer, rq);
    pthread_create(&c, nullptr, Producer, rq);

    pthread_join(c, nullptr);
    pthread_join(p, nullptr);

    return 0;
}